package com.heu.blood.analysis.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.heu.blood.analysis.exception.AnalysisExceptionEnum;
import com.heu.blood.analysis.vo.BloodSampleTransfusionDeliveryVo;
import com.heu.blood.analysis.vo.BloodSampleTransfusionReportVo;
import com.heu.blood.analysis.vo.BloodTestqueryPatientTestResultVo;
import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.globalExceptionHandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.heu.blood.analysis.entity.BloodSampleInfoTransfusionEntity;
import com.heu.blood.analysis.service.BloodSampleInfoTransfusionService;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;



/**
 * 输血申请患者血液样本信息表
 *
 * @author lhh
 * @email lhh1612325771@163.com
 * @date 2023-11-22 15:29:07
 */
@RestController
@RequestMapping("analysis/bloodsampleinfotransfusion")
public class BloodSampleInfoTransfusionController {
    @Autowired
    private BloodSampleInfoTransfusionService bloodSampleInfoTransfusionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "分页查询输血申请患者血液样本信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bloodSampleInfoTransfusionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "根据id查询输血申请患者血液样本信息")
    public R info(@PathVariable("id") Long id){
		BloodSampleInfoTransfusionEntity bloodSampleInfoTransfusion = bloodSampleInfoTransfusionService.getById(id);

        return R.ok().put("bloodSampleInfoTransfusion", bloodSampleInfoTransfusion);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "保存输血申请患者血液样本信息")
    public R save(@RequestBody List<BloodSampleInfoTransfusionEntity> bloodSampleInfoTransfusion){
		bloodSampleInfoTransfusionService.saveBatch(bloodSampleInfoTransfusion);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "更新输血申请患者血液样本信息")
    public R update(@RequestBody BloodSampleInfoTransfusionEntity bloodSampleInfoTransfusion){
		bloodSampleInfoTransfusionService.updateById(bloodSampleInfoTransfusion);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "根据id删除输血申请患者血液样本信息")
    public R delete(@RequestBody Long[] ids){
        bloodSampleInfoTransfusionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据bloodId查询血液样本信息
     */
    @RequestMapping("/selectSampleByBloodId")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "根据bloodId查询血液样本信息")
    public R selectSampleByBloodId(String bloodId){
        List<BloodSampleInfoTransfusionEntity> entities = bloodSampleInfoTransfusionService.selectSampleByBloodId(bloodId);

        return R.ok().put("data",entities);
    }

    /**
     * 根据bloodSampleId查询血液样本信息
     */
    @RequestMapping("/selectSampleByBloodSampleId")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "根据bloodSampleId查询血液样本信息")
    public R selectSampleByBloodSampleId(String bloodSampleId){
        List<BloodSampleInfoTransfusionEntity> entities = bloodSampleInfoTransfusionService.selectSampleByBloodSampleId(bloodSampleId);

        return R.ok().put("data",entities);
    }

    /**
     * 联表查询血液样本信息和病人信息
     */
    @RequestMapping("/selectSamplePatientReportQuery")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "联表查询血液样本信息和病人信息")
    public R selectSamplePatientReportQuery(BloodSampleTransfusionReportVo bloodSampleTransfusionReportVo) {
        PageUtils page = bloodSampleInfoTransfusionService.selectSamplePatientReportQuery(bloodSampleTransfusionReportVo);

        return R.ok().put("page", page);
    }

    @RequestMapping("/generateBloodSampleInfoTransfusionId")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "打印条形码接口生成相应的BloodSampleId")
    public R generateBloodSampleInfoTransfusionId(@RequestBody BloodSampleTransfusionDeliveryVo bloodSampleTransfusionDeliveryVo){
        BloodSampleInfoTransfusionEntity bloodSampleInfoTransfusionEntity = bloodSampleInfoTransfusionService.generateBloodSampleInfoTransfusionId(bloodSampleTransfusionDeliveryVo);
        return R.ok().put("data", bloodSampleInfoTransfusionEntity);
    }

    /**
     * 根据bloodSampleId更改血液样本信息的样本状态
     */
    @RequestMapping("/updateSampleStatusTORECEIVED")
    @ControllerLogAnnotation(module = "输血申请患者血液样本信息", operate = "根据bloodSampleId更改血液样本信息的样本状态")
    public R updateSampleStatusTORECEIVED(String bloodSampleId){
        int i = bloodSampleInfoTransfusionService.updateSampleStatusTORECEIVED(bloodSampleId);

        if (i == 0) {
            throw new MyException(AnalysisExceptionEnum.BLOOD_SAMPLE_ID_ILLEGAL_PARAMS);
        } else if(i == 1) {
            throw new MyException(AnalysisExceptionEnum.BLOOD_SAMPLE_ENTITY_NULL_POINTER);
        }
        return R.ok("血液样本状态更新为已接收");
    }

}
